/*
Copyright 2010 Johan Maasing

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package com.google.code.tretris.rendering;

import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

/**
 * Renderable object that is composed of parts. Subclasses should populate the
 * 'children' list with the parts that this object consists of. This can for
 * example be done in the constructor. This class is not thread safe.
 * 
 * @author "Johan Maasing" <johan@zoom.nu>
 * 
 */
public class MVComplexObject extends MVObject {
	/**
	 * The list of child objects that makes up this complex object. Subclasses
	 * should populate this list.
	 */
	protected final ArrayList<MVObject> children = new ArrayList<MVObject>();

	@Override
	public void setColor(float red, float green, float blue) {
		for (MVObject child : children) {
			child.setColor(red, green, blue);
		}
	}

	@Override
	public void setScale(float sx, float sy, float sz) {
		// We do not want to scale the complex object since that distorts the
		// child objects. This leads to a small suboptimization since the
		// complex object will put a scale factor of 1 on the modelview matrix
		// but we can live with that extra gl-call.
		for (MVObject child : children) {
			child.setScale(sx, sy, sz);
		}
	}

	@Override
	protected void renderShape(GL10 gl) {
		// We need to call render (instead of renderShape) to rotate/translate
		// the child objects.
		for (MVObject child : children) {
			child.render(gl);
		}
	}
}